from ws.config.constants import xunfei
from utils.spark.gpt import SparkApi

appid = xunfei.appid.value
api_secret = xunfei.api_secret.value
api_key = xunfei.api_key.value

domain = xunfei.domain3.value
Spark_url = xunfei.Spark_url3.value

text =[]

# length = 0

def getText(role,content):
    jsoncon = {}
    jsoncon["role"] = role
    jsoncon["content"] = content
    text.append(jsoncon)
    return text

def getlength(text):
    length = 0
    for content in text:
        temp = content["content"]
        leng = len(temp)
        length += leng
    return length

def checklen(text):
    while (getlength(text) > 8000):
        del text[0]
    return text
    
def gpt(websocket, text):
    question = checklen(getText("user", text))
    SparkApi.answer = ""
    print("星火:",end = "")
    SparkApi.main(websocket, appid, api_key, api_secret, Spark_url, domain, question)
    getText("assistant", SparkApi.answer)


if __name__ == '__main__':
    text.clear
    while(1):
        Input = input("\n" +"我:")
        question = checklen(getText("user", Input))
        SparkApi.answer = ""
        print("星火:",end = "")
        SparkApi.main(appid, api_key, api_secret, Spark_url, domain, question)
        getText("assistant", SparkApi.answer)
        # print(str(text))

